package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.EncyclopediaText;

public class GenericEncyclopediaTextDAO extends AutoKeys implements
		org.jetnuke.dao.EncyclopediaTextDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericEncyclopediaTextDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.EncyclopediaTextModel#retrieveByPK(int)
	 */
	public EncyclopediaText retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("EncyclopediaTextModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		EncyclopediaText s = new EncyclopediaText();

		if (rs.next()) {
			fillEncyTextFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	protected void fillEncyTextFromResultSet(EncyclopediaText s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillEncyTextFromResultSet(EncyclopediaText, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setCOUNTER(rs.getInt("COUNTER"));
		s.setEID(rs.getInt("EID"));
		s.setTEXT(rs.getString("TEXT"));
		s.setTID(rs.getInt("TID"));
		s.setTITLE(rs.getString("TITLE"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillEncyTextFromResultSet(EncyclopediaText, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.EncyclopediaTextModel#addNew(org.jetnuke.entities.EncyclopediaText)
	 */
	public int addNew(EncyclopediaText etext) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(EncyclopediaText) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("EncyclopediaTextModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("EncyclopediaTextModel.lastGeneratedEncyTextID"));
		initEncyText(etext, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(EncyclopediaText) - end"); //$NON-NLS-1$
		}
		return id;
	}

	/**
	 * @param etext
	 * @param p
	 */
	private void initEncyText(EncyclopediaText etext, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("initEncyText(EncyclopediaText, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setInt(1, etext.getCOUNTER());
		p.setInt(2, etext.getEID());
		p.setString(3, etext.getTEXT());
		p.setString(4, etext.getTITLE());

		if (logger.isDebugEnabled()) {
			logger
					.debug("initEncyText(EncyclopediaText, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.EncyclopediaTextModel#countByEid(int)
	 */
	public int countByEid(int eid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countByEid(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("EncyclopediaTextModel.countByEid"));
		p.setInt(1, eid);
		ResultSet rs = p.executeQuery();
		rs.next();

		int r = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countByEid(int) - end"); //$NON-NLS-1$
		}
		return r;
	}

}
